//+------------------------------------------------------------------+
//|                                              SimpleTrendEA.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// Input Parameters
input int      FastMA_Period = 10;        // Fast Moving Average Period
input int      SlowMA_Period = 20;        // Slow Moving Average Period
input double   LotSize = 0.1;             // Trading Lot Size
input int      StopLoss = 100;            // Stop Loss in points
input int      TakeProfit = 200;          // Take Profit in points

// Global Variables
int fastMA_Handle;
int slowMA_Handle;
double fastMA_Buffer[];
double slowMA_Buffer[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialize Moving Average indicators
    fastMA_Handle = iMA(_Symbol, PERIOD_CURRENT, FastMA_Period, 0, MODE_EMA, PRICE_CLOSE);
    slowMA_Handle = iMA(_Symbol, PERIOD_CURRENT, SlowMA_Period, 0, MODE_EMA, PRICE_CLOSE);
    
    if(fastMA_Handle == INVALID_HANDLE || slowMA_Handle == INVALID_HANDLE)
    {
        Print("Error creating indicators!");
        return(INIT_FAILED);
    }
    
    // Allocate memory for indicator buffers
    ArraySetAsSeries(fastMA_Buffer, true);
    ArraySetAsSeries(slowMA_Buffer, true);
    
    // Enable chart objects
    ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Release indicator handles
    IndicatorRelease(fastMA_Handle);
    IndicatorRelease(slowMA_Handle);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Update indicator buffers
    if(CopyBuffer(fastMA_Handle, 0, 0, 2, fastMA_Buffer) <= 0) return;
    if(CopyBuffer(slowMA_Handle, 0, 0, 2, slowMA_Buffer) <= 0) return;
    
    // Check if we already have an open position
    if(PositionSelect(_Symbol)) return;
    
    // Get current prices
    double speedHighPrice1 = iHigh(_Symbol, PERIOD_CURRENT, 1);
    double closePrice = iClose(_Symbol, PERIOD_CURRENT, 0);
    double speedLowPrice1 = iLow(_Symbol, PERIOD_CURRENT, 1);
    
    // Calculate SuperTrend value
    double superTrendValue = CalculateSuperTrend(speedHighPrice1, closePrice, speedHighPrice1, speedLowPrice1);
    
    // Display markers on the chart
    string highMarkerName = "HighPriceMarker";
    string closeMarkerName = "ClosePriceMarker";
    string lowMarkerName = "LowPriceMarker";
    
    // Delete existing markers
    ObjectDelete(0, highMarkerName);
    ObjectDelete(0, closeMarkerName);
    ObjectDelete(0, lowMarkerName);
    
    // Create new markers
    ObjectCreate(0, highMarkerName, OBJ_ARROW_DOWN, 0, TimeCurrent(), speedHighPrice1);
    ObjectCreate(0, closeMarkerName, OBJ_ARROW_UP, 0, TimeCurrent(), closePrice);
    ObjectCreate(0, lowMarkerName, OBJ_ARROW_UP, 0, TimeCurrent(), speedLowPrice1);
    
    // Set marker properties
    ObjectSetInteger(0, highMarkerName, OBJPROP_COLOR, clrRed);
    ObjectSetInteger(0, closeMarkerName, OBJPROP_COLOR, clrYellow);
    ObjectSetInteger(0, lowMarkerName, OBJPROP_COLOR, clrGreen);
    
    ObjectSetInteger(0, highMarkerName, OBJPROP_WIDTH, 2);
    ObjectSetInteger(0, closeMarkerName, OBJPROP_WIDTH, 2);
    ObjectSetInteger(0, lowMarkerName, OBJPROP_WIDTH, 2);
    
    // Add labels
    ObjectSetString(0, highMarkerName, OBJPROP_TEXT, "High: " + DoubleToString(speedHighPrice1, _Digits));
    ObjectSetString(0, closeMarkerName, OBJPROP_TEXT, "Close: " + DoubleToString(closePrice, _Digits));
    ObjectSetString(0, lowMarkerName, OBJPROP_TEXT, "Low: " + DoubleToString(speedLowPrice1, _Digits));
    
    // Trading logic
    if(fastMA_Buffer[1] > slowMA_Buffer[1] && fastMA_Buffer[0] <= slowMA_Buffer[0])
    {
        // Buy signal - Fast MA crosses above Slow MA
        double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
        double sl = ask - StopLoss * _Point;
        double tp = ask + TakeProfit * _Point;
        
        trade.Buy(LotSize, _Symbol, ask, sl, tp, "SimpleTrendEA Buy");
    }
    else if(fastMA_Buffer[1] < slowMA_Buffer[1] && fastMA_Buffer[0] >= slowMA_Buffer[0])
    {
        // Sell signal - Fast MA crosses below Slow MA
        double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
        double sl = bid + StopLoss * _Point;
        double tp = bid - TakeProfit * _Point;
        
        trade.Sell(LotSize, _Symbol, bid, sl, tp, "SimpleTrendEA Sell");
    }
}

//+------------------------------------------------------------------+
//| Trade class for order management                                 |
//+------------------------------------------------------------------+
CTrade trade; 